extends Node2D

@onready var path:PathFollow2D=$Path2D/PathFollow2D
@onready var sceneCamera:Camera2D=$Path2D/PathFollow2D/Camera2D
@onready var particle:GPUParticles2D=$GPUParticles2D
@export var camera:Camera2D
@export var tileMap :TileMap

var cameraMove=false
var hasPlayed=false
var playing=false

func _ready() -> void:
	var mapRect :=tileMap.get_used_rect()
	var tileSize:=tileMap.tile_set.tile_size
	sceneCamera.limit_top = mapRect.position.y*tileSize.y
	sceneCamera.limit_left = mapRect.position.x*tileSize.x
	sceneCamera.limit_right = mapRect.end.x*tileSize.x
	sceneCamera.limit_bottom = mapRect.end.y*tileSize.y
	sceneCamera.reset_smoothing()

func _physics_process(_delta: float) -> void:
	if !cameraMove:return
	if !playing: path.progress_ratio +=0.001
	if path.progress_ratio>=1:switchCamera()
	if path.progress_ratio>0.8 and !hasPlayed:smoke()

# 交换相机
func switchCamera():
	get_tree().paused=!get_tree().paused
	camera.enabled=!camera.enabled
	sceneCamera.enabled=!sceneCamera.enabled
	cameraMove=!cameraMove
	path.progress_ratio=0

# 切换粒子效果 
func toggelParticle():
	playing=!playing
	particle.emitting=!particle.emitting

# 打开烟雾
func smoke():
	hasPlayed=true
	toggelParticle()
	await get_tree().create_timer(1).timeout
	toggelParticle()
  

# 进入相机切换位置
func _on_area_2d_body_entered(body:Node2D) -> void:
	if body is Player:switchCamera()

